package org.wenhuayuan.project.main.fragment.home

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.layout_home_goods_list_item1.view.*
import org.wenhuayuan.library.util.HiDisplayUtil
import org.wenhuayuan.project.common.view.loadUrl
import org.wenhuayuan.project.main.R
import org.wenhuayuan.project.main.model.GoodsModel
import org.wenhuayuan.uilibrary.hiitem.HiDataItem

class GoodsItem(private val goodsModel: GoodsModel, private val hotTab:Boolean)
//    : HiDataItem<GoodsModel, HiViewHolder>(goodsModel){
    : HiDataItem<GoodsModel, RecyclerView.ViewHolder>(goodsModel){

    private val MAX_TAG_SIZE = 3

    override fun onBindData(holder: RecyclerView.ViewHolder, position: Int) {
        val context = holder.itemView.context
        holder.itemView.item_image.loadUrl(goodsModel.sliderImage!!)
        holder.itemView.item_title.text = goodsModel.goodsName

        holder.itemView.item_price.text = goodsModel.marketPrice
        holder.itemView.item_sale_desc.text = goodsModel.completedNumText

        val itemLabelContainer = holder.itemView.item_label_container
        if (!TextUtils.isEmpty(goodsModel.tags)) {
            val split = goodsModel.tags!!.split(" ")
            for (index in split.indices) {
                if (index > MAX_TAG_SIZE - 1) {
                    val childCount = itemLabelContainer.childCount
                    // 倒叙删除
                    for (index in childCount -1 downTo MAX_TAG_SIZE -1) {
                        itemLabelContainer.removeViewAt(index)
                    }
                    break
                }
                // 复用问题
                // 解决上下滑动复用问题 - 重复创建的问题
                val labelView = if (index > itemLabelContainer.childCount - 1)
                {
                    val view = createLabelView(context, index!=0)
                    itemLabelContainer.addView(view)
                    view
                }
                else {
                    itemLabelContainer.getChildAt(index) as TextView
                }

                labelView.text = split[index]
            }
        } else {
            itemLabelContainer.visibility = View.GONE
        }

        if (!hotTab) {
            val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
            val parentLeft = hiAdapter?.getAttachRecyclerView()?.left ?: 0
            val parentPaddingLeft = hiAdapter?.getAttachRecyclerView()?.paddingLeft ?: 0
            val itemLeft = holder.itemView.left
            if (itemLeft == (parentLeft + parentPaddingLeft)) {
                params.rightMargin = HiDisplayUtil.dp2px(2f)
            } else {
                params.leftMargin = HiDisplayUtil.dp2px(2f)
            }
            holder.itemView.layoutParams = params
        }
    }


    override fun getItemLayoutRes(): Int {
        return if (hotTab)R.layout.layout_home_goods_list_item1 else R.layout.layout_home_goods_list_item2
    }


    private fun createLabelView(context: Context, withLeftMargin: Boolean):TextView {
        val labelView = TextView(context)
        labelView.setTextColor(ContextCompat.getColor(context, R.color.color_dd2))
        labelView.textSize = HiDisplayUtil.dp2px(11f).toFloat()
        labelView.gravity = Gravity.CENTER
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,HiDisplayUtil.dp2px(14f))
        params.leftMargin = if (withLeftMargin) HiDisplayUtil.dp2px(5f)else 0
        labelView.layoutParams = params
        return labelView

    }

    override fun getSpanSize(): Int {
        return if(hotTab) super.getSpanSize() else 1
    }
}